package com.huawei.mpchartexample;

import com.huawei.mpchartlib.charts.LineChart;
import com.huawei.mpchartlib.components.Legend;
import com.huawei.mpchartlib.components.LimitLine;
import com.huawei.mpchartlib.components.XAxis;
import com.huawei.mpchartlib.components.YAxis;
import com.huawei.mpchartlib.data.Entry;
import com.huawei.mpchartlib.data.LineData;
import com.huawei.mpchartlib.data.LineDataSet;
import com.huawei.mpchartlib.formatter.IFillFormatter;
import com.huawei.mpchartlib.interfaces.dataprovider.LineDataProvider;
import com.huawei.mpchartlib.interfaces.datasets.ILineDataSet;
import com.huawei.mpchartlib.utils.ColorTemplate;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.Slider;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;

import java.util.ArrayList;

public class LineChartAbility extends Ability implements Slider.ValueChangedListener{

    private LineChart chart;
    private Slider sliderX, sliderY;
    private Text tvX, tvY;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_linechart);

        tvX = findComponentById(ResourceTable.Id_tvX);
        tvY = findComponentById(ResourceTable.Id_tvY);

        sliderX = findComponentById(ResourceTable.Id_sliderX);
        sliderX.setValueChangedListener(this);

        sliderY = findComponentById(ResourceTable.Id_sliderY);
        sliderY.setValueChangedListener(this);

        chart = findComponentById(ResourceTable.Id_lineChart);
        {   // // Chart Style // //

            // background color
            ShapeElement background= new ShapeElement();
            background.setShape(ShapeElement.RECTANGLE);
            background.setRgbColor(new RgbColor(255,255,255));
            chart.setBackground(background);

            // disable description text
            chart.getDescription().setEnabled(false);

            // enable touch gestures
            chart.setTouchEnabled(false);

            // set listeners
            chart.setOnChartValueSelectedListener(null);
            chart.setDrawGridBackground(false);

            // enable scaling and dragging
            chart.setDragEnabled(false);
            chart.setScaleEnabled(false);

            // force pinch zoom along both axis
            chart.setPinchZoom(false);
        }

        XAxis xAxis;
        {   // // X-Axis Style // //
            xAxis = chart.getXAxis();
            xAxis.setEnabled(true);
        }

        YAxis yAxis;
        {   // // Y-Axis Style // //
            yAxis = chart.getAxisLeft();

            // disable dual axis (only use LEFT axis)
            chart.getAxisRight().setEnabled(false);

            // axis range
            yAxis.setAxisMaximum(200f);
            yAxis.setAxisMinimum(-50f);

            yAxis.setDrawLabels(true);
        }

        {   // // Create Limit Lines // //
            LimitLine llXAxis = new LimitLine(0f, "Index 0");
            llXAxis.setLineWidth(1f);
            llXAxis.setLabelPosition(LimitLine.LimitLabelPosition.LEFT_BOTTOM);
            llXAxis.setTextSize(10f);
            llXAxis.setFont(Font.DEFAULT);

//            xAxis.addLimitLine(llXAxis);

            LimitLine ll1 = new LimitLine(150f, "Upper Limit");
            ll1.setLineWidth(2f);
            ll1.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
            ll1.setTextSize(10f);
            ll1.setFont(Font.DEFAULT);

            LimitLine ll2 = new LimitLine(-30f, "Lower Limit");
            ll2.setLineWidth(2f);
            ll2.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_BOTTOM);
            ll2.setTextSize(10f);
            ll1.setFont(Font.DEFAULT);

            yAxis.addLimitLine(ll1);
            yAxis.addLimitLine(ll2);
        }

        // add data
        sliderX.setProgressValue(45);
        sliderY.setProgressValue(180);
        setData(45, 180);
        chart.invalidate();

        {   // // Legend // //
            // get the legend (only possible after setting data)
            Legend l = chart.getLegend();
            // draw legend entries as lines
            l.setForm(Legend.LegendForm.LINE);
        }

        // draw chart
        Component.DrawTask drawTask = new Component.DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                chart.onDraw(canvas);
            }
        };
        chart.addDrawTask(drawTask);
    }

    private void setData(int count, float range) {

        ArrayList<Entry> values = new ArrayList<>();

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range) - 30;
            values.add(new Entry(i, val));
        }

        LineDataSet set1;

        if (chart.getData() != null &&
                chart.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) chart.getData().getDataSetByIndex(0);
            set1.setValues(values);
            set1.notifyDataSetChanged();
            chart.getData().notifyDataChanged();
            chart.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(values, "DataSet 1");

            set1.setDrawIcons(false);

            // black lines and points
            set1.setColor(Color.BLACK.getValue());
            set1.setCircleColor(Color.BLACK.getValue());

            // line thickness and point size
            set1.setLineWidth(1f);
            set1.setCircleRadius(3f);

            // draw points as solid circles
            set1.setDrawCircleHole(false);

            // customize legend entry
            set1.setFormLineWidth(1f);
            set1.setFormSize(15.f);

            // text size of values
            set1.setValueTextSize(9f);

            // set the filled area
            set1.setDrawFilled(true);
            set1.setFillFormatter(new IFillFormatter() {
                @Override
                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                    return chart.getAxisLeft().getAxisMinimum();
                }
            });

            // set color of filled area
            set1.setFillColor(Color.BLACK.getValue());

            ArrayList<ILineDataSet> dataSets = new ArrayList<>();
            dataSets.add(set1); // add the data sets

            // create a data object with the data sets
            LineData data = new LineData(dataSets);

            // set data
            chart.setData(data);
        }
    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {
        tvX.setText(String.valueOf(sliderX.getProgress()));
        tvY.setText(String.valueOf(sliderY.getProgress()));
        setData(sliderX.getProgress(), sliderY.getProgress());
        // redraw
        chart.invalidate();
    }
}
